package cn.team11.smarteye.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import cn.team11.sds.dayi.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.team11.smarteye.activity.DeviceConfigActivity;
import cn.team11.smarteye.base.BaseFragment;
import cn.team11.smarteye.base.BasePresenter;
import cn.team11.smarteye.misc.SConstants;
import cn.team11.smarteye.utils.AppLogger;

/**
 * Created by Yang on 2017/10/28.
 */

public class QrcodeNetworkFragment extends BaseFragment{

    @BindView(R.id.qrcode_image)
    ImageView mQrcodeImage;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_view_device_qrcode, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mFragmentListener = (FragmentListener) getActivity();
        mFragmentListener.setTitle(getResources().getString(R.string.device_qrcode_txt));
    }

    public static QrcodeNetworkFragment newInstance(Bundle bundle) {
        QrcodeNetworkFragment fragment = new QrcodeNetworkFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        AppLogger.i("onViewCreated");
        init();
    }


    @Override
    public void onResume() {
        super.onResume();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void init() {

        String ssid = getArguments().getString(SConstants.WIFI_NAME);
        String pwd = getArguments().getString(SConstants.WIFI_PWD);

        Bitmap qrcode = ((DeviceConfigActivity)getActivity()).createQrcodeImage(ssid, pwd);
        mQrcodeImage.setImageBitmap(qrcode);
    }


    @OnClick(R.id.btn_net_next)
    public void netNext(View view){
        getActivity().getSupportFragmentManager().beginTransaction()
                .hide(this)
                .add(R.id.bind_fragment_content, DeviceBindingFragment.newInstance(null))
                .addToBackStack(DeviceBindingFragment.class.getSimpleName())
                .commitAllowingStateLoss();
    }

    @Override
    public void setPresenter(BasePresenter presenter) {

    }
}
